﻿<%@ Page Title="" Language="C#" MasterPageFile="~/website/Site1.Master" AutoEventWireup="true" %>

<%@ Import Namespace="ChicagoDealsN" %>
<%@ Import Namespace="System.Data" %>
<script language="c#" runat="Server">
    
    private int getId()
    {
        return int.Parse(Request.QueryString["id"]);
    }
    
    void Page_Load()
    {
        if (!IsPostBack)
        {
            try
            {
                int itemID = getId();
                string id = Request.QueryString["Deal_Id"];
                float savings;
                
                BizDeals myBizObject = new BizDeals();

                DataTable dt = myBizObject.BLGetDealById(itemID);

                foreach (DataRow dr in dt.Rows)
                {
                    
                    lblName.Text = dr["Deal_Title"].ToString();
                    lblPrice.Text = dr["Deal_Price"].ToString();
                    lblPDesc.Text = dr["Deal_Description"].ToString();
                    string img= "~"+dr["Img_Path"].ToString();
                    dealImg.ImageUrl = this.ResolveUrl(img);
                    savings = calculateSavings(float.Parse(dr["Org_Price"].ToString()), float.Parse(dr["Deal_Price"].ToString()));
                    dealSavings.Text = "Your saving " + ((int)savings).ToString() + "%";
                }

            }
            catch (Exception ex)
            {
                confirmation.Text = "<h3>Invalid request</h1><br/>" + ex + "<br/><br/>";
            }
        }
    }

    private float calculateSavings(float op, float dp)
    { 
        return (100 * (op-dp))/op;
    }
    
    public void Purchase_Button_Click(Object s, EventArgs e)
    {
        BizPurchaseHistory bpl = new BizPurchaseHistory();
        DataUser du = Session["UserDet"] as DataUser;
        if (du == null)
        {
            confirmation.Text = "Please, login to purchase this deal!";
        }
        else
        {
            int x = (Session["UserDet"] as DataUser).Balance;
            
            int y = (int)float.Parse(lblPrice.Text.Trim());
            int balToSetAfterPurchase = 0;
            
            if(y>0)
            {
                balToSetAfterPurchase = x - y;

                if (balToSetAfterPurchase > 0)
                {
                    bool purchased = bpl.purchaseDeal(du.User_Id, getId(), balToSetAfterPurchase);
                    if (purchased)
                    {
                        confirmation.Text = "Congratulations! You've Purchased a Deal!";
                        (Session["UserDet"] as DataUser).Balance = balToSetAfterPurchase;
                    }
                }
                else
                {
                    confirmation.Text = "You don't have sufficient balance!";
                }
            }
            else {
                confirmation.Text = "Balance is not in correct format!";
            }
        }
    }

    private bool IsAdmin()
    {
        bool isAdmin = false;
        if (Session["UserDet"] != null)
        {
            DataUser usr = Session["UserDet"] as DataUser;
            return usr.Is_Admin == 0 ? false : true;
        }
        return isAdmin;
    }

    private bool IsLoggedIn()
    {
        return (Session["UserDet"] != null) ? true : false;
    }

</script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="conirmation-container">
       <asp:Label id="confirmation" runat="server" />
       <br/>
    
</div>
    
    
    <table>
        <tr>
            <td valign="top">
                <div id="deal-img-container"><asp:Image ID="dealImg" runat="server" /></div>            
            </td>
            <td valign="top">
                <h1><asp:Label ID="lblName" runat="server"></asp:Label></h1>
                <h2 class="savingsStyle"><asp:Label ID="dealSavings" runat="server" /></h2>
                <table id="dealdetail-container">
                    
                    <tr>
                        <td>
                            Points :
                        </td>
                        <td>
                            <asp:Label ID="lblPrice" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Description
                        </td>
                        <td>
                            <asp:Label ID="lblPDesc" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td>
                            <asp:Button ID="btnPurchase" runat="server" Text="Get It!" type="Submit" OnClick="Purchase_Button_Click" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    
  
        <%--<a href="~/website/alldeals.aspx">Get all</a></div>--%>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    
</asp:Content>
